package com.xdclass.concurrent;

/**
 * @Author: Timo
 * @Date: 2019/4/25 11:53
 * @Description: 死锁练习
 */
public class DeadLockDemo {
    private static final Object HAIR_A = new Object();
    private static final Object HAIR_B = new Object();


    /**
     * 这里模拟的是线程A等待线程B释放资源
     * 线程B等待线程A释放资源造成的死锁现象
     * @param args
     */
    public static void main(String[] args){
        new Thread(()->{
            synchronized (HAIR_A) {
                try {
                    Thread.sleep(50L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (HAIR_B) {
                    System.out.println("A成功抓住了B的头发");
                }
            }
        }).start();


        new Thread(()->{
            synchronized (HAIR_B) {

                synchronized (HAIR_A) {
                    System.out.println("B成功抓住了A的头发");
                }
            }
        }).start();
    }
}
